//
//  MainViewController.m
//  UI05_delegate模式
//
//  Created by Scott on 15/4/23.
//  Copyright (c) 2015年 www.lanou3g.com 蓝欧3G. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
#pragma mark - 知识点1  delegate模式
/**
 * @brief 讲解
 * 
 * 当一个类的某些功能需要别人来实现, 但是既不明确是些什么功能, 又不明确谁来实现这些功能的时候, delegate模式就可以派上用场.
 * 目的是为了降低类之间的耦合性
 * delegate 也是用来解耦的, 它不再简单让目标去执行一个动作了.
 * delegate 可以去处理一些列事件, 就像UITextFieldDelegate一样, 能检测将要开始编辑, 已经开始编辑, return按钮点击等等.
 * 
 * delegate 使用场景:
    控件有一系列时间点, 控制器可以实现这个代理方法, 以便在适当的时机做适当的事.
 */
    
    /* 创建两个textField 对象 */
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 60, self.view.frame.size.width - 40, 40)];
    textField.borderStyle = 3;
    textField.placeholder = @"请输入用户名";
    [self.view addSubview:textField];
    [textField release];
    textField.delegate = self;
    
    
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(20, 150, self.view.frame.size.width - 40, 40)];
    textField2.borderStyle = 3;
    textField2.placeholder = @"请输入密码";
    [self.view addSubview:textField2];
    [textField2 release];
    textField2.delegate = self;
    
}

/* 这些方法一般来实现检测功能 
 * 控件有一系列时间点, 控制器可以实现这个代理方法, 以便在适当的时机做适当的事.
 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"准备输入");
    return YES; /* 如果return NO, 实现不能输入的效果 */
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"开始输入");
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"将要完成编辑");
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"已经完成编辑");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
